package com.yunpeng.aidevice.biz.service.impl;

import com.yunpeng.aidevice.biz.service.IDictionariesBizService;
import com.yunpeng.aidevice.common.vo.DictionariesVO;
import com.yunpeng.aidevice.dao.service.IDictionariesService;
import com.yunpeng.aidevice.common.constant.QueryConstants;
import com.yunpeng.common.dto.Result;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.common.enums.QueryOperatorEnum;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.paging.query.QueryParam;
import com.yunpeng.aidevice.dao.model.record.DictionariesDO;
import com.yunpeng.aidevice.dao.service.IListQueryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * DictionariesBizServiceImpl 字典业务服务实现
 *
 * @author : Moore(hemw)
 * @date : 2020-04-13
 */
@Service
public class DictionariesBizServiceImpl implements IDictionariesBizService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DictionariesBizServiceImpl.class);

    @Resource
    private IDictionariesService dictionariesService;

    @Resource
    private IListQueryService listQueryService;

    @Override
    /**
     *
     * 根据业务域和字典组查询字典数据集合
     *
     * @param domain 业务域
     * @param group  字典组
     * @return
     */
    public Result<List<DictionariesVO>> queryDictionaries(String domain, String group) {
        List<DictionariesDO> dictionariesDOList = dictionariesService.queryDictionariesList(domain, group);
        if (dictionariesDOList == null || dictionariesDOList.isEmpty()) {
            LOGGER.debug("could not get dictionaries list,is empty,domain {} group {} ", domain, group);
            return Result.success(new ArrayList<>());
        }
        return Result.success(dictionariesDOList.stream().map(t -> new DictionariesVO(t.getKeyDomain(), t.getKeyGroup(), t.getKeyName(), t.getKeyValue(), t.getKeyComment(), t.getKeySeq())).collect(Collectors.toList()));
    }

    @Override
    /**
     * 更新字典
     *
     * @param domain 域
     * @param group  组
     * @param key    键
     * @param value  值
     * @return
     */
    public Result<Void> updateDictionaries(String domain, String group, String key, String value) {
        if (dictionariesService.updateDictionaries(domain, group, key, value)) {
            return Result.success();
        }
        return Result.fail(ResultCodeEnum.SAVE_DATA_FAILED);
    }

    @Override
    /**
     * 字典分页列表
     *
     * @param domain      业务域（左匹配）
     * @param group       组（左匹配）
     * @param key         键（左匹配）
     * @param pageSize    （页数据数量，1-100，默认10）
     * @param currentPage （当前页码，默认1）
     * @return
     */
    public Result<PagingQueryDTO> queryDictionariesList(String domain, String group, String key, Integer pageSize, Integer currentPage) {
        PagingQueryDTO pagingQueryDTO = new PagingQueryDTO();
        List<QueryParam> queryParams = new ArrayList<>();
        LinkedHashMap<String, String> orderByMap = new LinkedHashMap<>(16);
        //设置查询条件
        if (StringUtils.isNotBlank(domain)) {
            queryParams.add(QueryParam.quickSetQueryParam("key_domain", domain, QueryOperatorEnum.LEFT_LIKE));
        }
        //设置查询条件
        if (StringUtils.isNotBlank(group)) {
            queryParams.add(QueryParam.quickSetQueryParam("key_group", group, QueryOperatorEnum.LEFT_LIKE));
        }
        if (StringUtils.isNotBlank(key)) {
            queryParams.add(QueryParam.quickSetQueryParam("key_name", key, QueryOperatorEnum.LEFT_LIKE));
        }
        queryParams.add(QueryParam.quickSetQueryParam("flag", 1, QueryOperatorEnum.EQUALS));
        //设置排序
        orderByMap.put("update_time", "desc");
        orderByMap.put("id", "desc");
        pagingQueryDTO.setCurrentPage(currentPage);
        pagingQueryDTO.setPageSize(pageSize);
        pagingQueryDTO.setConditionList(queryParams);
        pagingQueryDTO.setSortColumnMap(orderByMap);
        PagingQueryDTO resultData = listQueryService.query(pagingQueryDTO, QueryConstants.QUERY_DICTIONARIES_LIST_4_SERVICE_SIDE, null);
        return Result.success(resultData);
    }

}
